// HomeNet Device LCD - 2 line
// 12-13-2010 <mdoll@email.usf.edu> http://opensource.org/licenses/mit-license.php

#include <HomeNetDevices.h>
#include "HomeNetDeviceLCD.h"
#include <WProgram.h>

void HomeNetDeviceLCD::init(uint8_t location) {
#ifdef DEBUG
    Serial.println("Init LCD");
#endif
    HomeNetDevicePortI2C::init(location);
    _displayCount = 0;

    _lcd = new LiquidCrystalI2C(*_port);
    _lcd->begin(16, 2);
    _lcd->setCursor(0, 0);
    _lcd->print("LCD at ");
    _lcd->print(location, DEC);
    delay(1000);
    _lcd->clear();
}

HomeNetPartialPacket* HomeNetDeviceLCD::process(const uint16_t& fromNode, const uint8_t& fromDevice, const uint8_t& command, const HomeNetPayload& payload) {
#ifdef DEBUG
    Serial.println("Process LCD");
#endif

    //receive outside packets
    if (fromNode != _homeNet->_nodeId) {
        for (uint8_t i = 0; i < _displayCount; i++) {
            if ((fromNode == _display[i].fromNode) && (fromDevice == _display[i].fromDevice)) {
#ifdef DEBUG
                    Serial.println("LCD Display Packet Matched");
#endif
                 _lcd->setCursor(0, _display[i].row);
                 char line[16];// = "                ";
                 char num[9];
                //display = &_display[i];
                switch (command) {

                    case CMD_BYTE:
                        //_lcd->print("Byte");
                        //_lcd->print(payload.getByte(),DEC);
                        sprintf(line,_display[i].message, itoa(payload.getByte(), num, 10));
                        break;

                    case CMD_INT:
                        //_lcd->print("Int");
                        //_lcd->print(payload.getInt(),DEC);
                        sprintf(line,_display[i].message, itoa(payload.getInt(), num, 10));
                        break;

                    case CMD_FLOAT:
                        //_lcd->print(_display[i].message);
                        //_lcd->print();
                        sprintf(line,_display[i].message, dtostrf(payload.getFloat(), 5, 2,num));
                        break;
                    
                }
                _lcd->print(line);
                break;
            } 
        }
    }

    switch (command) {

        case CMD_SETVALUE:
           
            uint8_t length;
            length = payload.length;
            _lcd->clear();
            _lcd->setCursor(0, 0);
            if (length <= 16) {
                for (uint8_t i = 0; i < length; i++) {
                    _lcd->print(payload.data[i]);
                }
                break;
            }
            for (uint8_t i = 0; i < 16; i++) {
                _lcd->print(payload.data[i]);
            }
            _lcd->setCursor(0, 1);
            for (uint8_t i = 16; i < length; i++) {
                _lcd->print(payload.data[i]);

            }
            break;

        case CMD_ON:
            _lcd->backlight();
            break;

        case CMD_OFF:
            _lcd->noBacklight();
            break;
    }
    return NULL;
}

void HomeNetDeviceLCD::registerDisplay(Display display[], uint8_t count){
    _displayCount = count;
    _display = display;
}